package blog.controller;

import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import blog.entities.User;
import blog.service.UserService;
import blog.service.impl.UserServiceImpl;


@WebServlet("/login")
public class UserController extends HttpServlet {

	private UserService userService = new UserServiceImpl();
	
	private String username;
	private String password;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 用于处理客户请求中的用户登录
	 * @param req 请求
	 * @param resp 响应
	 * @throws ServletException
	 * @throws IOException
	 */
	private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		if (req.getParameter("login")!= null && req.getParameter("login").equals("登录")) {
			User user = userService.userLogin(username, password);
			if (user != null) {
				if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
					System.out.println("login()方法-登录成功");
					
					// 获取session对象
					HttpSession session = req.getSession();
					// 将User对象放到session中
					session.setAttribute("user", user);

					// 访问用户信息
					List<User> allUser = userService.getAllUser();
//					for (User user2 : allUser) {
//						System.out.println(user2);
//					}
					if(allUser != null && allUser.size() != 0) {
						session.setAttribute("allUser", allUser);
						// 重定向到某个指定网页
						req.getRequestDispatcher("/WEB-INF/success.jsp").forward(req,resp);
						resp.getWriter().write("<p>login success...</p>" + "成功");
					}
					

					
					// 使用转发定位到指定网页
//					System.out.println(req.getContextPath());
//					resp.sendRedirect(req.getContextPath() + "/modifyUser?option=modify");
					
				} else {
					System.out.println("登录失败");
					resp.getWriter().write("<p>login fail...</p>" + "失败");
				}
			} else {
				System.out.println("登录失败");
				resp.getWriter().write("<p>login fail...</p>" + "失败");
			}
		} 
	}
	
	/**
	 * 用于处理客户请求中的用户注册
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	private void register(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException  {
		
		if(req.getParameter("register") != null && req.getParameter("register").equals("注册")) {
			System.out.println("注册过程...");
			System.out.println(userService.userRegister(username,password));
			System.out.println("登录过程...");
			User user = userService.userLogin(username, password);
			System.out.println(user);
			resp.getWriter().write("<p>login success...</p>" + "成功");
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		// 解决浏览器到服务器端中文编码不一致问题
		req.setCharacterEncoding("UTF-8");
		// 解决服务器端到浏览器中文编码不一致问题
		resp.setContentType("text/html;charset=utf-8");
		
		username = req.getParameter("username");
		password = req.getParameter("password");
		// 确保login和register方法中使用this.username和this.password不会出现NullPointer异常
		if(username != null && password != null) {
			if(req.getParameter("login") != null) {
				login(req,resp);
			} else if(req.getParameter("register") != null) {
				register(req,resp);
			}
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

}
